package com.sptus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sptus.aop.AppIntercept;
import com.sptus.entity.SptUser;
import com.sptus.entity.SptUserEquipment;
import com.sptus.entity.SptUserParent;
import com.sptus.service.IAppEquipmentService;
import com.sptus.service.ISptUserEquipmentService;
import com.sptus.service.ISptUserParentService;
import com.sptus.system.domain.Equipment;
import com.sptus.system.domain.vo.AppInfo;
import com.sptus.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "app监控接口")
@RequestMapping("/sys/app/Monitoring")
public class DevMonitoringController {
    @Autowired
    IAppEquipmentService equipmentService;

    @Autowired
    ISptUserEquipmentService sptUserEquipmentService;


    @Autowired
    ISptUserParentService sptUserParentService;

    @GetMapping("/getEquipmentInfoData")
    @ApiOperation(value="获取设备详情接口", notes="获取监控详情接口")
    @AppIntercept
    public Result<Object> getEquipmentInfoData(AppInfo appInfo,SptUser sptUser){





        LambdaQueryWrapper<SptUserEquipment> sptUserEquipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserEquipmentLambdaQueryWrapper.eq(SptUserEquipment::getEquipmentId,appInfo.getSn());
        sptUserEquipmentLambdaQueryWrapper.eq(SptUserEquipment::getUserId,sptUser.getId());
        SptUserEquipment sptUserEquipment = sptUserEquipmentService.getBaseMapper().selectOne(sptUserEquipmentLambdaQueryWrapper);

        LambdaQueryWrapper<Equipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(Equipment::getSn,appInfo.getSn());
        Equipment equipment = equipmentService.getBaseMapper().selectOne(equipmentLambdaQueryWrapper);

        if(sptUserEquipment == null ){
            return Result.error("Device does not exist");
        }
        if(!equipment.getStatus().equals("1")){
            return Result.ok();
        }

        Result<Object> result= equipmentService.getEquipmentInfoData(appInfo,equipment);
        Integer id = sptUser.getId();
        return result.success("Query success");
    }

    @GetMapping("/getEquipmentInfo")
    @ApiOperation(value="获取设备数据接口", notes="获取设备数据接口")
    @AppIntercept
    public Result<Object> getEquipmentInfo(AppInfo appInfo,SptUser sptUser){

        ArrayList<String> ids = new ArrayList<>();
        LambdaQueryWrapper<SptUserParent> sptUserParentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getUserId,sptUser.getId());
        sptUserParentLambdaQueryWrapper.eq(SptUserParent::getState,"1");
        List<HashMap<String,String>> sptUserParents = sptUserParentService.selectListInfo(sptUserParentLambdaQueryWrapper);
        if(sptUserParents.size()>0){
            ids.add(sptUserParents.get(0).get("parent_id"));
        }
        ids.add(sptUser.getId()+"");


        LambdaQueryWrapper<SptUserEquipment> sptUserEquipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserEquipmentLambdaQueryWrapper.eq(SptUserEquipment::getEquipmentId,appInfo.getSn());
        sptUserEquipmentLambdaQueryWrapper.in(SptUserEquipment::getUserId,ids);

        SptUserEquipment sptUserEquipment = sptUserEquipmentService.getBaseMapper().selectOne(sptUserEquipmentLambdaQueryWrapper);
        LambdaQueryWrapper<Equipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(Equipment::getSn,appInfo.getSn());
        Equipment equipment = equipmentService.getBaseMapper().selectOne(equipmentLambdaQueryWrapper);

        if(sptUserEquipment == null ){
            return Result.error("Device does not exist");
        }
        if(!equipment.getStatus().equals("1")){
            return Result.ok();
        }
        Result<Object> result= equipmentService.getEquipmentInfo(appInfo,equipment);
        return result.success("Query success");
    }
    @GetMapping("/getMonitoringAlarm")
    @ApiOperation(value="获取告警列表接口", notes="获取设备数据接口")
    @AppIntercept
    public Result<Object> getMonitoringInfo(AppInfo appInfo,SptUser sptUser){
        Result<Object> result = new Result<>();
        Integer id = sptUser.getId();
        return result.success("Query success");
    }
    @GetMapping("/getInverterInfo")
    @ApiOperation(value="获取告警列表接口", notes="获取设备数据接口")
    @AppIntercept
    public Result<Object> getInverterInfo(AppInfo appInfo,SptUser sptUser){
        LambdaQueryWrapper<SptUserEquipment> sptUserEquipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sptUserEquipmentLambdaQueryWrapper.eq(SptUserEquipment::getEquipmentId,appInfo.getSn());
        sptUserEquipmentLambdaQueryWrapper.eq(SptUserEquipment::getUserId,sptUser.getId());
        SptUserEquipment sptUserEquipment = sptUserEquipmentService.getBaseMapper().selectOne(sptUserEquipmentLambdaQueryWrapper);
        LambdaQueryWrapper<Equipment> equipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        equipmentLambdaQueryWrapper.eq(Equipment::getSn,appInfo.getSn());
        Equipment equipment = equipmentService.getBaseMapper().selectOne(equipmentLambdaQueryWrapper);

        if(sptUserEquipment == null ){
            return Result.error("Device does not exist");
        }

        if(!equipment.getStatus().equals("1")){
            return Result.ok();
        }
        Result<Object> result= equipmentService.getInverterInfo(appInfo,equipment);
        return result.success("send successfully");
    }


}
